angular.module('tab-visinfo', [])

.controller 'TabVisinfoCtrl', ($localStorage,Modal,wfConfig,inWeixin,$location,$scope,$rootScope,Toast, $state, $ionicHistory,$http,$ionicViewSwitcher, userData,$stateParams) ->
  $scope.editing = false
  $scope.setDefault = (e,info)->
    e.stopPropagation()
    e.preventDefault()
    $http.put('/visitInfo/'+info.id,{isSelect:1})
    .success (resp) ->
      Toast.showMsg("已设置为默认就诊信息")
      _.each $scope.infoList,(item)->
        item.isSelect = false
      info.isSelect = true
    .error (resp, status) ->
      Toast.showBusinessError(resp.message)

  $scope.canEdit = ->
    $scope.editing = !$scope.editing
  $scope.goEditinfo= (e,info) ->
    e.stopPropagation()
    if $scope.editing
      $localStorage.ifEdit=0;
      $localStorage.editInfo=info
      $localStorage.editOrAdd="编辑";
      $state.go("tab.editinfo")
    else
      if $localStorage.visinfo.fromYuyue
        _.extend $localStorage.visinfo,info
        $localStorage.visinfo.fromYuyue = false
        $state.go("tab.yuyue")
      else
        #编辑辨识，0为编辑，1为新增
        $localStorage.ifEdit=0;
        $localStorage.editInfo=info
        $localStorage.editOrAdd="编辑";
        $state.go("tab.editinfo")
  #获取就诊信息
  $http.get('/visitInfo')
    .success (resp) ->
      $scope.infoList=resp.message.content
      j = 0
      while j < resp.message.content.length
        $scope.infoList[j].no = j
        j++
      console.log($scope.infoList)
    .error (resp, status) ->
     # console.log(resp)
      Toast.showBusinessError(resp.message)
  #返回
  $scope.back= ->
    if $localStorage.visinfo.fromYuyue
      $ionicHistory.goBack()
    else
      $state.go('tab.myself')
  #新增就诊信息
  $scope.addInfo= ->
    $localStorage.ifEdit=1;
    $localStorage.editOrAdd="新增";
    $state.go("tab.editinfo")
  $scope.remove = (e,info)->
    e.stopPropagation()
    $http.delete('/visitInfo/'+info.id)
    .success (resp) ->
      Toast.showMsg("删除信息成功")
      $scope.infoList = _.without $scope.infoList,info
    .error (resp, status) ->
      console.log(resp)
      Toast.showBusinessError(resp.message)